﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace ManageATrois
{
    public partial class ProfileDialog : Form
    {

        public ProfileManager pm;

        public ProfileCallback DialogClosed;

        private int lastSelectedIndex;

        public ProfileDialog()
        {
            InitializeComponent();
        }

        public void Init()
        {
            // either load the profile manager... or create a new one here if none exist!
            try
            {
                if (!LoadProfileManager())
                    pm = new ProfileManager();
            }
            catch (System.Exception ex)
            {
                pm = new ProfileManager();
            }

            lastSelectedIndex = -1;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selInd = listBox1.SelectedIndex;

            // prevent infinate recursion 
            if (selInd == lastSelectedIndex)
                return;

            if (selInd < 0)
                NoProfileSelected();
            else
            {
                if ( lastSelectedIndex != -1 )
                    SaveProfileIndexData(lastSelectedIndex, false);

                PopulateProfileData(selInd);
            }

            UpdateFromProfileManager();

            if (selInd >= 0)
            {
                lastSelectedIndex = selInd;
                listBox1.SelectedIndex = selInd;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // New..
            int index = pm.CreateNewEmptyProfile();
            if (index != -1)
            {
                pm.SelectProfile(index);
                PopulateProfileData(index);
            }

            UpdateFromProfileManager();

            if ( index != -1 )
                listBox1.SelectedIndex = index;
        }

        private void ProfileDialog_Load(object sender, EventArgs e)
        {
            NoProfileSelected();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            // Done..
            DialogClosed();
            this.Hide();
        }

        private void UpdateFromProfileManager()
        {
            listBox1.Items.Clear();
            for (int i = 0; i < pm.profileCount; i++)
            {
                if ( pm.selectedIndex == i )
                    listBox1.Items.Add(pm.profiles[i].Name + " <--- ");
                else
                    listBox1.Items.Add(pm.profiles[i].Name);
            }
        }

        private void NoProfileSelected()
        {
            SelectBtn.Enabled = false;
            SourceSetBtn.Enabled = false;
            DestSetBtn.Enabled = false;
            SaveBtn.Enabled = false;
            DeleteBtn.Enabled = false;

            ProfileTextBox.Enabled = false;
            ProfileTextBox.ReadOnly = true;
        }

        private void PopulateProfileData( int index )
        {
            if (index < 0)
                return;

            SelectBtn.Enabled = true;
            SaveBtn.Enabled = true;
            SourceSetBtn.Enabled = true;
            DestSetBtn.Enabled = true;
            DeleteBtn.Enabled = true;

            ProfileTextBox.Enabled = true;
            ProfileTextBox.ReadOnly = false;

            ProfileTextBox.Text = pm.profiles[index].Name;
            SourceTextBox.Text = pm.profiles[index].SourceFolder;
            DestTextBox.Text = pm.profiles[index].DestFolder;
        }

        private bool SaveProfileIndexData(int index, bool displayErrorMsg = true)
        {
            bool success = true;
            pm.profiles[index].Name = ProfileTextBox.Text;
            pm.profiles[index].SourceFolder = SourceTextBox.Text;
            pm.profiles[index].DestFolder = DestTextBox.Text;

            if (displayErrorMsg)
            {
                if ((ProfileTextBox.Text == "") || (ProfileTextBox.Text == "new.."))
                {
                    MessageBox.Show(("Please give profile index " + index.ToString() + " a name.. "));
                    success = false;
                }
                else if (SourceTextBox.Text == "not set..")
                {
                    MessageBox.Show(("Please give profile index " + index.ToString() + " a SourceFolder.. "));
                    success = false;
                }
                else if (DestTextBox.Text == "not set..")
                {
                    MessageBox.Show(("Please give profile index " + index.ToString() + " a Destination Folder.. "));
                    success = false;
                }
            }

            return success;
        }

        private bool SaveAllProfileData( bool displayErrorMsg = true )
        {
            bool success = true;
            for (int i = 0; i < pm.profileCount; i++)
                success = success && SaveProfileIndexData(i, displayErrorMsg );

            return success;
        }

        private void SourceSetBtn_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                SourceTextBox.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void DestSetBtn_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                DestTextBox.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            int selInd = listBox1.SelectedIndex;
            if (SaveProfileIndexData(selInd))
            {
                SaveProfileManager();
                UpdateFromProfileManager();
            }

            if (selInd != -1)
            {
                lastSelectedIndex = selInd;
                listBox1.SelectedIndex = selInd;
            }
        }

        private bool LoadProfileManager()
        {

            XmlSerializer deserializer = new XmlSerializer(typeof(ProfileManager));
            TextReader textReader = new StreamReader(@"profiles.xml");
            pm = (ProfileManager)deserializer.Deserialize(textReader);
            textReader.Close();

            UpdateFromProfileManager();

            listBox1.ClearSelected();

            return true;
        }

        private void SaveProfileManager()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ProfileManager));
            TextWriter textWriter = new StreamWriter(@"profiles.xml");
            serializer.Serialize(textWriter, pm);
            textWriter.Close();
        }

        private void SelectBtn_Click(object sender, EventArgs e)
        {
            int selInd = listBox1.SelectedIndex;
            pm.SelectProfile(listBox1.SelectedIndex);
            UpdateFromProfileManager();

            if (selInd != -1)
                listBox1.SelectedIndex = selInd;
        }

        private void DeleteBtn_Click(object sender, EventArgs e)
        {
            pm.DeleteProfile(listBox1.SelectedIndex);
            lastSelectedIndex = -1;
            UpdateFromProfileManager();
        }

        public Profile GetSelectedProfile()
        {
            if (pm.selectedIndex == -1)
                return null;

            return pm.profiles[pm.selectedIndex];
        }
    }
}
